package xin.lhuo.we.server.dao;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Component;
import xin.lhuo.we.server.entity.Folder;

import java.util.Date;
import java.util.List;

/**
 * @author Lhuo
 * @date 2019/5/25 15:01
 */
@Mapper
@Component
public interface FolderMapper {

    @Insert("insert into we_folder (user_id,name,create_time,update_time) values(#{userId},#{name},#{createTime},#{updateTime})")
    void saveFolder(Folder folder);

    @Select("select count(1) from we_folder where user_id =#{userId} and name = #{name}")
    int checkFolderByNameAndUserId(@Param("userId") Integer userId, @Param("name") String name);

    @Select("select * from we_folder where user_id = #{userId} order by id desc")
    @Results(id = "folder",
            value = {
                    @Result(column = "id", property = "id", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
                    @Result(column = "user_id", property = "userId", javaType = Integer.class, jdbcType = JdbcType.INTEGER),
                    @Result(column = "name", property = "name", javaType = String.class, jdbcType = JdbcType.VARCHAR),
                    @Result(column = "create_time", property = "createTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP),
                    @Result(column = "update_time", property = "updateTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP)
            })
    List<Folder> getFolderByUserId(@Param("userId") Integer userId);

    @Delete("delete from we_folder where user_id =#{userId} and id = #{id}")
    void deleteFolder(@Param("userId") Integer userId, @Param("id") Integer id);

    @Update("update we_folder set name =#{name} where id = #{id}")
    void renameFolder(@Param("id") Integer id, @Param("name") String name);

    @Select("select count(1) from we_folder where user_id = #{userId} and id = #{id}")
    int checkFolderByIdAndUserId(@Param("userId") Integer userId, @Param("id") Integer id);

    @Select("select count(1) from we_folder where user_id = #{userId}")
    int countByUserId(Integer userId);

}
